package com.rear.controller;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rear.components.PrintComponent;
import com.rear.entity.XatLogEntity;
import com.rear.service.XatLogService;
import com.rear.utils.DateUtil;
import com.rear.utils.PageUtils;
import com.rear.utils.R;


/**
 * 
 * 
 * @author October
 * @email chenzeming@9an-data.com
 * @date 2017-03-28 11:09:08
 */
@Controller
@RequestMapping("xatlog")
public class XatLogController {
	
	@Autowired
	private XatLogService xatLogService;
	
	@RequestMapping("/xatlog.html")
	public String list(){
		return "xatlog/xatlog.html";
	}
	@RequestMapping("/chartLine.html")
	public String chartLine(){
		return "xatlog/chartLine.html";
	}
	@RequestMapping("/open-flash-chart.swf")
	public String openFlashChart(){
		return "xatlog/open-flash-chart.swf";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("xatlog:list")
	public R list(
			String czzt ,
			String createTimeSatrt ,
			String createTimeEnd ,
			Integer page, 
			Integer limit){	
		Map<String, Object> map = new HashMap<>();
		if(czzt!=null&&czzt!=""){			
			czzt = czzt.replaceAll("\"","\\\"");
		}
		map.put("czzt",czzt);
		map.put("createTimeSatrt",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToStartDataLong(createTimeSatrt)),"yyyy-MM-dd"));
		map.put("createTimeEnd",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToEndDataLong(createTimeEnd)),"yyyy-MM-dd"));
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//查询列表数据
		List<XatLogEntity> xatLogList = xatLogService.queryList(map);
		int total = xatLogService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(xatLogList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("xatlog:info")
	public R info(@PathVariable("id") Integer id){
		XatLogEntity xatLog = xatLogService.queryObject(id);
		
		return R.ok().put("xatLog", xatLog);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("xatlog:save")
	public R save(@RequestBody XatLogEntity xatLog){
		xatLogService.save(xatLog);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("xatlog:update")
	public R update(@RequestBody XatLogEntity xatLog){
		xatLogService.update(xatLog);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("xatlog:delete")
	public R delete(@RequestBody Integer[] ids){
		xatLogService.deleteBatch(ids);
		
		return R.ok();
	}
	
	
	/**
	 * 下载列表
	 */
	@ResponseBody
	@RequestMapping(value = "/list/download.json", method = RequestMethod.GET)
	@RequiresPermissions("xatlog:list")
	public R download(
			String czzt ,
			String createTimeSatrt ,
			String createTimeEnd,
			HttpServletResponse response,
			HttpServletRequest request
			)throws Exception {
		
		
		if(czzt==null||czzt==""||"null".equals(czzt)){		
			System.err.println(1);
			return R.error("操作主题不可为空");
		}
		if(createTimeSatrt==null||createTimeSatrt==""||"null".equals(createTimeSatrt)){	
			System.err.println(2);
			return R.error("查询时间不可为空");
		}
		if(createTimeEnd==null||createTimeEnd==""||"null".equals(createTimeEnd)){
			System.err.println(3);
			return R.error("查询时间不可为空");
		}
	
		Map<String, Object> map = new HashMap<>();
		czzt = czzt.replaceAll("\"","\\\"");
		map.put("czzt",czzt);
		map.put("createTimeSatrt",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToStartDataLong(createTimeSatrt)),"yyyy-MM-dd"));
		map.put("createTimeEnd",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToEndDataLong(createTimeEnd)),"yyyy-MM-dd"));
		List<XatLogEntity> xatLogList = xatLogService.queryList(map);
		
		String logName=PrintComponent.createFile(xatLogList, "xatLogList", "日志记录", "xatLogList.xml");
		
		return R.ok(logName);
		
	}
	
	/**
	 * 下载列表
	 */
	@ResponseBody
	@RequestMapping(value = "/list/download/xls.json", method = RequestMethod.GET)
	public R downloadxls(
			String fileName ,
			OutputStream out, HttpServletRequest req, HttpServletResponse resp)throws Exception {
		System.err.println(fileName);
		PrintComponent.downDocument(fileName, out, resp);
		return R.ok();
	}

}
